package com.kmproject.haifu.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.jess.arms.widget.alertview.AlertView;
import com.jess.arms.widget.alertview.AlertViewOnItemClickListener;
import com.jess.arms.widget.autolayout.AutoToolbar;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.di.component.DaggerMyStoreLookObtainedComponent;
import com.kmproject.haifu.mvp.contract.MyStoreLookObtainedContract;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.beans.HouseRentListShowInfoBean;
import com.kmproject.haifu.mvp.model.entity.beans.MyStoreMainBean;
import com.kmproject.haifu.mvp.model.entity.beans.ObtainedHouseBean;
import com.kmproject.haifu.mvp.model.entity.beans.UserBean;
import com.kmproject.haifu.mvp.model.entity.resbean.HouseParmarBean;
import com.kmproject.haifu.mvp.presenter.MyStoreLookObtainedPresenter;
import com.kmproject.haifu.mvp.ui.adapter.ObtainedHouseAdapter;
import com.kmproject.haifu.mvp.ui.widget.ScaleTransitionPagerTitleView;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 08/02/2019 14:07
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class MyStoreLookObtainedActivity extends BaseActivity<MyStoreLookObtainedPresenter> implements MyStoreLookObtainedContract.View {

    @BindView(R.id.toolbar_backImg)
    ImageView toolbarBackImg;
    @BindView(R.id.toolbar_back)
    RelativeLayout toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.back_right_icon)
    ImageView backRightIcon;
    @BindView(R.id.back_right_txt)
    TextView backRightTxt;
    @BindView(R.id.toolbar_right)
    RelativeLayout toolbarRight;
    @BindView(R.id.toolbar)
    AutoToolbar toolbar;
    @BindView(R.id.mystore_story_indicator)
    MagicIndicator mystoreStoryIndicator;
    @BindView(R.id.mystore_head_typeselect_RadioButton1)
    RadioButton mystoreHeadTypeselectRadioButton1;
    @BindView(R.id.mystore_head_typeselect_RadioButton2)
    RadioButton mystoreHeadTypeselectRadioButton2;
    @BindView(R.id.mystore_head_typeselect_RadioGroup)
    RadioGroup mystoreHeadTypeselectRadioGroup;
    @BindView(R.id.mystore_story_middle_lay)
    LinearLayout mystoreStoryMiddleLay;
    @BindView(R.id.mystore_story_middle_RecyclerView)
    RecyclerView mystoreStoryMiddleRecyclerView;

    MyStoreMainBean myStoreMainBean;
    private List<HousePayWayBean> mhousePayWayBeans;//房屋类型，居民住宅、商品房屋
    private HouseParmarBean houseParmarBean=new HouseParmarBean();
    private ObtainedHouseAdapter obtainedHouseAdapter;
    private int selectType=0;
    UserBean userBean;
    LinearLayout mystore_story_lay;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMyStoreLookObtainedComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_my_store_look_obtained; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mystore_story_lay=findViewById(R.id.mystore_story_lay);
        if (getIntent().hasExtra("HousePayWayBean"))
        {
            mhousePayWayBeans=(List<HousePayWayBean>) getIntent().getSerializableExtra("HousePayWayBean");
            houseParmarBean.setHouseCategory(mhousePayWayBeans.get(0).getCategory());
        }
        if (getIntent().hasExtra("MyStoreMainBean"))
        {
            myStoreMainBean=(MyStoreMainBean)getIntent().getSerializableExtra("MyStoreMainBean");
        }


        toolbarTitle.setText(ArmsUtils.getString(this, R.string.mystory_xxfy));
        toolbarTitle.setTextColor(ArmsUtils.getColor(this, R.color.white));
        toolbar.setBackgroundColor(ArmsUtils.getColor(this, R.color.color_3F3F4B));
        toolbarBackImg.setImageResource(R.drawable.member_center_main_back_img);


        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return 2;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(index==0?("租房(0)"):("售房(0)"));

                    simplePagerTitleView.setWidth((ScreenUtils.getScreenWidth())/2);
                simplePagerTitleView.setTextSize(14);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.color_191919));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.color_191919));
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//                layoutParams.setMargins(15, 0, 5, 0);//4个参数按顺序分别是左上右下
                simplePagerTitleView.setLayoutParams(layoutParams);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mystoreStoryIndicator.onPageSelected(index);
                        commonNavigator.getAdapter().notifyDataSetChanged();
                    }
                });

                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                linePagerIndicator.setLineHeight(8.0f);
                linePagerIndicator.setColors(ArmsUtils.getColor(MyStoreLookObtainedActivity.this, R.color.color_F9D35A));

                return linePagerIndicator;
            }

            @Override
            public float getTitleWeight(Context context, int index) {
                return 1.0f;
            }

        });
        mystoreStoryIndicator.setNavigator(commonNavigator);
        if (mhousePayWayBeans!=null&&!mhousePayWayBeans.isEmpty())
        {
            houseParmarBean.setHouseCategory(mhousePayWayBeans.get(selectType).getCategory());
        }
        mystoreStoryMiddleRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        obtainedHouseAdapter=new ObtainedHouseAdapter(new ArrayList<>(), new ObtainedHouseAdapter.ItemClickCallBack() {
            @Override
            public void Callback(HouseRentListShowInfoBean houseRentListShowInfoBean) {
                new AlertView("提示", "确定删除此内容？", "取消", new String[]{"删除"}, null, MyStoreLookObtainedActivity.this, AlertView.Style.Alert, new AlertViewOnItemClickListener() {

                    @Override
                    public void onItemClick(Object o, int position) {
                        if (position == AlertView.CANCELPOSITION) {
                            //点击取消按钮

                        }else {
                            mPresenter.removeStore(houseRentListShowInfoBean.getRentId());
                        }
                    }
                }).show();
            }
        });
        mystoreStoryMiddleRecyclerView.setAdapter(obtainedHouseAdapter);

        houseParmarBean.setLimit(AppConstant.pageSize_big);
       if (getIntent().hasExtra("type")&&getIntent().getStringExtra("type").equals("userObtained"))
       {
           mystore_story_lay.setVisibility(View.GONE);
            userBean = DataHelper.getDeviceData(this, AppConstant.USER_INFO);//获取用户信息
           mPresenter.listHouseRentShelvesForUser(userBean.getUserId(),houseParmarBean);

       }else {
           mPresenter.listHouseRentShelves(myStoreMainBean.getStoreId(),houseParmarBean);//房屋的关注
       }

        mystoreHeadTypeselectRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId)
                {
                    case R.id.mystore_head_typeselect_RadioButton1:
                        selectType=0;
                        if (mhousePayWayBeans!=null&&!mhousePayWayBeans.isEmpty())
                        {
                            houseParmarBean.setHouseCategory(mhousePayWayBeans.get(selectType).getCategory());
                        }
                        obtainedHouseAdapter.setNewData(null);
                        houseParmarBean.setPage(1);
                        if (getIntent().hasExtra("type")&&getIntent().getStringExtra("type").equals("userObtained"))
                        {

                            houseParmarBean.setHouseCategory(mhousePayWayBeans.get(0).getCategory());
                            mPresenter.listHouseRentShelvesForUser(userBean.getUserId(),houseParmarBean);
                        }else {
                            mPresenter.listHouseRentShelves(myStoreMainBean.getStoreId(),houseParmarBean);
                        }

                      break;
                    case R.id.mystore_head_typeselect_RadioButton2:
                        selectType=1;
                        if (mhousePayWayBeans!=null&&!mhousePayWayBeans.isEmpty())
                        {
                            houseParmarBean.setHouseCategory(mhousePayWayBeans.get(selectType).getCategory());
                        }
                        obtainedHouseAdapter.setNewData(null);
                        houseParmarBean.setPage(1);
                        if (getIntent().hasExtra("type")&&getIntent().getStringExtra("type").equals("userObtained"))
                        {
                            houseParmarBean.setHouseCategory(mhousePayWayBeans.get(1).getCategory());
                            mPresenter.listHouseRentShelvesForUser(userBean.getUserId(),houseParmarBean);
                        }else {
                            mPresenter.listHouseRentShelves(myStoreMainBean.getStoreId(),houseParmarBean);
                        }

                        break;
                }

            }
        });
    }

    @Override
    public void showLoading() {
              loadingDialog.show();
    }

    @Override
    public void hideLoading() {
        loadingDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void listHouseRentShelvesSuccess(ResultBean<List<ObtainedHouseBean>> obtainedHouseBeans) {
          if ((obtainedHouseBeans==null||obtainedHouseBeans.getData()==null||obtainedHouseBeans.getData().isEmpty())&&obtainedHouseAdapter.getData().isEmpty())
          {
              obtainedHouseAdapter.setEmptyView(AppUtil.setEmptyView(MyStoreLookObtainedActivity.this,"您还没有下架信息","",null,true));
              return;
          }

        obtainedHouseAdapter.setNewData(obtainedHouseBeans.getData());
    }

    @Override
    public void removeStoreSuccess() {
        //删除房源
        mystoreStoryMiddleRecyclerView.post(new Runnable() {
            @Override
            public void run() {
                showMessage("删除成功");
            }
        });

         houseParmarBean=new HouseParmarBean();
        if (mhousePayWayBeans!=null&&!mhousePayWayBeans.isEmpty())
        {
            houseParmarBean.setHouseCategory(mhousePayWayBeans.get(selectType).getCategory());
        }
        if (getIntent().hasExtra("type")&&getIntent().getStringExtra("type").equals("userObtained"))
        {
            UserBean userBean = DataHelper.getDeviceData(this, AppConstant.USER_INFO);//获取用户信息
            mPresenter.listHouseRentShelvesForUser(userBean.getUserId(),houseParmarBean);
        }else {
            obtainedHouseAdapter.setNewData(null);
            mPresenter.listHouseRentShelves(myStoreMainBean.getStoreId(),houseParmarBean);
        }


    }
}
